﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace MTQ.BLL.UploadExcel
{
    public class UploadService
    {
        public delegate void UploadDelegate(string fileExtension, Stream stream, ExcelHelper.ImportExcelDelegate importDelegate, DataTable dt, out string Errormessage);
        public bool UploadXls(ExcelHelper.ImportExcelDelegate ExcuteExcelSql, string filepath, DataTable dt, out string ErrorMessage)
        {
            string message = string.Empty;
            bool result = false;

            try
            {
                // 文件上传后的保存路径
                //HttpPostedFileBase fileData = fileDatas[0];
                FileStream inputStream = new FileStream(filepath, FileMode.Open);
                string fileName = Path.GetFileName(filepath);// 原始文件名称
                string fileExtension = Path.GetExtension(fileName); // 文件扩展名
                UploadDelegate upload = new UploadDelegate(ExcelHelper.UploadExcel2007);
                //upload.BeginInvoke(fileExtension, fileData.InputStream, ExcuteExcelSql, out message, null, null);
                upload.Invoke(fileExtension, inputStream, ExcuteExcelSql, dt, out message);
                // ExcelHelper.UploadExcel2007(fileExtension, fileData.InputStream, ExcuteExcelSql, out message);
                result = true;
            }
            catch (Exception ex)
            {
                message += "导入格式不正确！";
                result = false;
            }
            ErrorMessage = message;
            return result;
        }


        public delegate void UploadDelegateDataSet(string fileExtension, Stream stream, ExcelHelper.ImportExcelDelegateDataset importDelegate, DataTable dt, DataSet ds, out string Errormessage);

        public static string debuggerstring = "";
        public bool UploadXlsDataset(ExcelHelper.ImportExcelDelegateDataset ExcuteExcelSql, string filepath, DataTable dt, DataSet ds, out string ErrorMessage)
        {
            string message = string.Empty;
            bool result = false;

            try
            {
                // 文件上传后的保存路径
                //HttpPostedFileBase fileData = fileDatas[0];
                FileStream inputStream = new FileStream(filepath, FileMode.Open);
                string fileName = Path.GetFileName(filepath);// 原始文件名称
                string fileExtension = Path.GetExtension(fileName); // 文件扩展名
                UploadDelegateDataSet upload = new UploadDelegateDataSet(ExcelHelper.UploadExcel2007DataSet);
                //upload.BeginInvoke(fileExtension, fileData.InputStream, ExcuteExcelSql, out message, null, null);
                upload.Invoke(fileExtension, inputStream, ExcuteExcelSql, dt,  ds, out message);
                // ExcelHelper.UploadExcel2007(fileExtension, fileData.InputStream, ExcuteExcelSql, out message);
                result = true;
            }
            catch (Exception ex)
            {
                message += "导入格式不正确！" + debuggerstring;
                Common.LogManager.WriteLog("数据导入异常 "+ message+","+ex.Message);
                result = false;
            }
            ErrorMessage = message;
            return result;
        }
    }
}
